<?php
function p($arr)
{
    echo '<pre>' . print_r($arr, true) . '</pre>';
}

function isNotBlankString($C_char)
{
    if (!is_string($C_char)) return false; //判断是否是字符串类型
    if (empty($C_char)) return false; //判断是否已定义字符串
    if ($C_char == '') return false; //判断字符串是否为空
    return true;
}

function isMobi($C_char)
{
    if (!is_string($C_char)) return false; //判断是否是字符串类型
    if (empty($C_char)) return false; //判断是否已定义字符串
    if ($C_char == '') return false; //判断字符串是否为空
    if (preg_match("/^((1[0-9]{2})+\d{8})$/", $C_char)) {
        return true;
    } else {
        return false;
    }
}

function create_uuid($prefix = "")
{    //可以指定前缀
    $str = md5(uniqid(mt_rand(), true));
    $uuid = substr($str, 0, 8) . '-';
    $uuid .= substr($str, 8, 4) . '-';
    $uuid .= substr($str, 12, 4) . '-';
    $uuid .= substr($str, 16, 4) . '-';
    $uuid .= substr($str, 20, 12);
    return $prefix . $uuid;
}

function outJson($json = "")
{
    header("Content-Type:text/html;charset=UTF-8");
    echo(json_encode($json));
}

function outError($message = "")
{
    $json['message'] = $message;
    $json['success'] = false;
    header("Content-Type:text/html;charset=UTF-8");
    echo json_encode($json);
}

function outSuccess($message = "")
{
    $json['message'] = $message;
    $json['success'] = true;
    header("Content-Type:text/html;charset=UTF-8");
    echo json_encode($json);
}

function getFromUrl($url = "", $data = "")
{
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
        echo 'Errno' . curl_error($curl);//捕抓异常
    }
    curl_close($curl); // 关闭CURL会话
    return $tmpInfo; // 返回数据
}

function jsonToArr($jsonStr)
{
    $arr = array();
    if (strlen($jsonStr) > 0) {
        $e = substr($jsonStr, strlen($jsonStr) - 1, strlen($jsonStr));
        $s = substr($jsonStr, 0, 1);
        if ($s == "{" && $e == "}") {
            $jsonStr = substr($jsonStr, 1, strlen($jsonStr) - 2);
            $arr1 = split(",", $jsonStr);
            foreach ($arr1 as $a) {
                $k = substr($a, 0, strpos($a, ":"));
                $v = substr($a, strpos($a, ":") + 1);
                $arr2 = array($k => $v);
                $arr = array_merge($arr, $arr2);
            }
        }
        return $arr;
    }
}

//DES 加密
function encrypt($encrypt, $key = "")
{
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
    $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt, MCRYPT_MODE_ECB, $iv);
    $encode = base64_encode($passcrypt);
    return $encode;
}

//DES 解密
function decrypt($decrypt, $key = "")
{
    $decoded = base64_decode($decrypt);
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv);
    return $decrypted;
}

function startsWith($str, $sub)
{
    return !strncmp($str, $sub, strlen($sub));
}

function endsWith($str, $sub)
{
    $length = strlen($sub);
    if ($length == 0) {
        return true;
    }

    return (substr($str, -$length) === $sub);
}


/**
 * 简单对称加密算法之加密
 * @param String $string 需要加密的字串
 * @param String $skey 加密EKY
 * @author
 * @date 2013-08-13 19:30
 * @update 2014-10-10 10:10
 * @return String
 */
function encode($string = '', $skey = 'cdwphp')
{
    $strArr = str_split(base64_encode($string));
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key < $strCount && $strArr[$key] .= $value;
    return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
}

/**
 * 简单对称加密算法之解密
 * @param String $string 需要解密的字串
 * @param String $skey 解密KEY
 * @author Anyon Zou <zoujingli@qq.com>
 * @date 2013-08-13 19:30
 * @update 2014-10-10 10:10
 * @return String
 */
function decode($string = '', $skey = 'cdwphp')
{
    $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key <= $strCount && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
    return base64_decode(join('', $strArr));
}

?>